﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Model;
using System.IO;
using University.Workshop.Business;
using University.Workshop.DataAccess;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Web.Security;


namespace University.Workshop.WebApp
{
    public partial class RateProduct : System.Web.UI.Page
    {
        public Product selectedProductItem = null;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
                FormsAuthentication.RedirectToLoginPage();
            
            name.Text = Request["ProductName"];
            string itemID = Request["ProductID"];
            ProductID.Text = itemID;
            Category.Text = Request["ProductCategory"];
            GridView1.DataSource = ViewProductManager.LoadCompleteInformation(ProductID.Text);
            GridView1.DataBind();
            Product prod = ProductDataAccess.GetProductById(Convert.ToInt64(ProductID.Text));
            this.selectedProductItem = prod;
            LoadInformation(prod);
            decimal av = RatingDataAccess.GetProductRatingsAverage(long.Parse(itemID));
            average.Text = av.ToString();

            if (av == 0)
            {
                image22.Visible = true;
                image33.Visible = true;
                image44.Visible = true;
                image55.Visible = true;
                image66.Visible = true;
            }

            if (av == 1)
            {
                image2.Visible = true;
                image33.Visible = true;
                image44.Visible = true;
                image55.Visible = true;
                image66.Visible = true;
            }

            if (av == 2)
            {
                image2.Visible = true;
                image3.Visible = true;
                image44.Visible = true;
                image55.Visible = true;
                image66.Visible = true;
            }

            if (av == 3)
            {
                image2.Visible = true;
                image3.Visible = true;
                image4.Visible = true;
                image55.Visible = true;
                image66.Visible = true;
            }

            if (av == 4)
            {
                image2.Visible = true;
                image3.Visible = true;
                image4.Visible = true;
                image5.Visible = true;
                image66.Visible = true;
            }

            if (av == 5)
            {
                image2.Visible = true;
                image3.Visible = true;
                image4.Visible = true;
                image5.Visible = true;
                image6.Visible = true;
            }
        }
        public void LoadInformation(Product prod)
        {

            if (Directory.Exists(Server.MapPath("~/Thumbnails/") + prod.ProductId + "/"))
            {
                DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Thumbnails/") + prod.ProductId + "/");
                FileInfo[] currentImages = dir.GetFiles();
                string thumbUrl = "~/Thumbnails/" + prod.ProductId + "/";
                foreach (FileInfo fi in currentImages)
                {
                    if (String.IsNullOrEmpty(image1.ImageUrl))
                    {
                        image1.ImageUrl = thumbUrl + fi.Name;

                    }
                }
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            Response.Redirect("ViewProduct.aspx");
        }

        protected void SaveComment_Click(object sender, EventArgs e)
        {        
            Rating rate = new Rating();
            rate.ProductId = long.Parse(ProductID.Text);
            rate.Comment = comment.Text;
            rate.UserId = UserDataAccess.GetUserByEmail(User.Identity.Name).Id;
            rate.Date = DateTime.Now;
            if (RatingDataAccess.UserAlreadyRated(rate))
            {
                RatingDataAccess.InsertComment(rate);
                errorMsg.Text = "Comment Saved";
                errorMsg.Visible = true;
            }
            else
            {
                errorMsg.Text = "You must rate the product before you comment it";
                errorMsg.Visible = true;
            }

            //Response.Redirect("RateProduct.aspx?ProductID=" + ProductID.Text + "&ProductName=" + name.Text + "&ProductCategory=" + Category.Text);

        }
    }
}